<template>
  <dialog-editor ref="dialog" :width="80"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      dataFormConfig: {
        id: {
          hidden: true
        },
        orderDetailId: {
          label: '产品订单编号',
          list: [],
          type: 'list',
          placeholder: '请选择订单信息'
        },
        productId: {
          label: '商品信息',
          list: [],
          type: 'list',
          placeholder: '请选择商品信息'
        },
        customerRequire:{
          label: '规格描述',
        },
        productWeight: {
          label: '克重',
          type: 'number',
          max: 1000,
          default: 0
        },
        productVolume: {
          label: '容量',
          type: 'number',
          max: 1000,
          default: 0
        },
        strawLength:{
          label: '吸管长度',
          type: 'number',
          max: 1000,
          default: 0
        },
        supplierId: {
          reply: () => this.dataForm.id,
          label: '供应商',
          list: [],
          type: 'list',
          placeholder: '请选择供应商'
        },
        supplierProductNo: {
          label: '供应商产品编号',
        },
        unitPrice: {
          label: '价格',
          type: 'number',
          selectCallback: this.numberChanged
        },
        taxInclusive: {
          label: '含税价',
          type: 'radio',
          radioBorder: false,
          radioList: [
            {label: 0, text: '不含税'},
            {label: 1, text: '含税'}
          ],
          default: 0
        },
        purchaseNumber: {
          label: '采购数量',
          type: 'number',
          max: 10000000,
          selectCallback: this.numberChanged
        },
        paymentCycle:{
          label: '付款周期',
          type: 'radio',
          radioBorder: false,
          radioList: [
            {label: 0, text: '月结'},
            {label: 1, text: '款到发货'},
            {label: 2, text: '3个月账期'},
            {label: 3, text: '付30%定金，余款70%款到发货'},
            {label: 4, text: '2个月账期'},
          ],
          default: 0
        },
        totalPrice: {
          label: '总价'
        },

        sampleImage: {
          label: '产品图片',
          type: 'uploadScreenshot',
          imageType: 'uploadProductImage'
        },
        productDrawingId: {
          label: '产品图纸',
          type: 'uploadImage',
          imageType: 'uploadProductPaper'
        },
        boxPackText: {
          label: '箱唛描述',
          placeholder: '请输入箱唛'
        },
        boxPackImage: {label: '箱唛图片',
          type: 'uploadImage',
          imageType: 'uploadProductImage'
        },
        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        },
        status: {
          label: '采购状态',
          type: 'radio',
          radioBorder: false,
          radioList: [
            {text: '采购退回', label: -5},
            {text: '待采购', label: 0},
            {text: '采购中', label: 2},
            {text: '采购逾期预警', label: 3},
            {text: '采购已逾期', label: 4},
            {text: '货已完工',label: 5},
            {text: '采购暂停', label: 6},
            {text: '入库进行中', label: 7},
            {text: '入库完成', label: 8},
            {text: '入库完成-其它库', label: 15},
            {text: '特采', label: 20},
            {text: '采购取消', label: 99},
          ],
          default: 0
        }
      }
    }
  },
  methods: {
    initSupplierId() {
      mRequest('supplierApi/getSupplierListByParentId', {}, (res) => {
        this.dataFormConfig['supplierId'].list = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    // initBottleSuitAndCapList() {
    //   mRequest('systemApi/getSysDictLabelTreeList', {}, (res) => {
    //     this.dataFormConfig['sysDictIdList'].list = this.R.pathOr([], ['data', 'list'], res)
    //   })
    // },
    initContractOrderTreeList() {
      mRequest('orderApi/getOrderDetailList', {}, (res) => {
        this.dataFormConfig['orderDetailId'].list = this.R.pathOr([], ['data', 'list'], res)
      })
    },

    numberChanged() {
      const purchaseNumber = this.R.propOr(0, 'purchaseNumber', this.dataForm)
      const unitPrice = this.R.propOr(0, 'unitPrice', this.dataForm)
      this.dataForm.totalPrice = purchaseNumber * unitPrice
    },

    init(id, property, cb, submitAtFinish = true) {
      this.dataForm.id = id || 0
      this.initProductInfoList()
      this.initSupplierList('supplierId')
      this.initContractOrderTreeList('orderDetailId')
      this.$refs.dialog.initDialog({
        submitAtFinish,
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule( 'purchaseNumber','productId', 'status'),
        initApi: 'purchaseApi/purchasePartInfo',
        saveApi: 'purchaseApi/savePurchasePart',
        updateApi: 'purchaseApi/updatePurchasePart',
        objectName: 'purchaseProduct',
        submitCallback: cb
      })

      this.$nextTick(() => {
        if (!this.R.isNil(property)) {
          Object.keys(property).forEach((key) => {
            this.dataForm[key] = property[key]
          })
        }
      })
    }
  }
}
</script>
